package org.wishwork.api.supplier.upload;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wishwork.api.Api;
import org.wishwork.api.supplier.query.SupplierQueryApi;
import org.wishwork.context.Context;
import org.wishwork.helper.HttpHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 供应商上传
 * @author: WangYX
 * @create: 2022-04-08 16:13
 * @Version: 1.0.0
 **/
public class SupplierUploadApi extends Api<List<SupplierUploadResponse>, List<SupplierUploadRequest>> {

    private static final Logger log = LoggerFactory.getLogger(SupplierUploadApi.class);

    private Context context;

    public SupplierUploadApi(Context context) {
        this.context = context;
    }

    @Override
    protected String query(List<SupplierUploadRequest> supplierUploadRequests) throws Exception {
        String url = "/open/supplier/upload";
        List<Map<String, Object>> list = new ArrayList<>();
        if (supplierUploadRequests != null && supplierUploadRequests.size() > 0) {
            for (SupplierUploadRequest supplierUploadRequest : supplierUploadRequests) {
                Map<String, Object> map = new HashMap<>();
                if (StrUtil.isNotEmpty(supplierUploadRequest.getAddress())) {
                    map.put("address", supplierUploadRequest.getAddress());
                }
                if (StrUtil.isNotEmpty(supplierUploadRequest.getBeginDate())) {
                    map.put("begin_date", supplierUploadRequest.getBeginDate());
                }
                if (StrUtil.isNotEmpty(supplierUploadRequest.getContacts())) {
                    map.put("contacts", supplierUploadRequest.getContacts());
                }
                if (ObjectUtil.isNotEmpty(supplierUploadRequest.getEnabled())) {
                    map.put("enabled", supplierUploadRequest.getEnabled());
                }
                if (StrUtil.isNotEmpty(supplierUploadRequest.getMobile())) {
                    map.put("mobile", supplierUploadRequest.getMobile());
                }
                if (StrUtil.isNotEmpty(supplierUploadRequest.getName())) {
                    map.put("name", supplierUploadRequest.getName());
                }
                if (StrUtil.isNotEmpty(supplierUploadRequest.getPhone())) {
                    map.put("phone", supplierUploadRequest.getPhone());
                }
                if (StrUtil.isNotEmpty(supplierUploadRequest.getRemark())) {
                    map.put("remark", supplierUploadRequest.getRemark());
                }
                if (StrUtil.isNotEmpty(supplierUploadRequest.getRemark2())) {
                    map.put("remark2", supplierUploadRequest.getRemark2());
                }
                if (StrUtil.isNotEmpty(supplierUploadRequest.getRemark3())) {
                    map.put("remark3", supplierUploadRequest.getRemark3());
                }
                if (StrUtil.isNotEmpty(supplierUploadRequest.getSupplierCode())) {
                    map.put("supplier_code", supplierUploadRequest.getSupplierCode());
                }
                if (StrUtil.isNotEmpty(supplierUploadRequest.getDepositbank())) {
                    map.put("depositbank", supplierUploadRequest.getDepositbank());
                }
                if (StrUtil.isNotEmpty(supplierUploadRequest.getBankacount())) {
                    map.put("bankacount", supplierUploadRequest.getBankacount());
                }
                if (StrUtil.isNotEmpty(supplierUploadRequest.getAcountnumber())) {
                    map.put("acountnumber", supplierUploadRequest.getAcountnumber());
                }
                if (StrUtil.isNotEmpty(supplierUploadRequest.getGroup())) {
                    map.put("group", supplierUploadRequest.getGroup());
                }
                list.add(map);
            }
        }
        String biz = JSONObject.toJSONString(list);
        return HttpHelper.sendRequest(biz, url);
    }

    @Override
    protected List<SupplierUploadResponse> process(JSONObject data, List<SupplierUploadRequest> supplierUploadRequests) throws Exception {
        JSONArray datas = data.getJSONArray("datas");
        List<SupplierUploadResponse> supplierUploadResponses = datas.toJavaList(SupplierUploadResponse.class);
        return supplierUploadResponses;
    }
}
